package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Bed;
import com.woniu.entity.Ward;
import com.woniu.mapper.BedMapper;
import com.woniu.mapper.WardMapper;
import com.woniu.service.BedService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.websocket.WardWebSocket;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {
    @Resource
    private BedMapper bedMapper;
    @Resource
    private WardMapper wardMapper;
    @Override
    public ResponseUtil findAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Bed> bedList = bedMapper.findAll();
        PageInfo pageInfo = new PageInfo(bedList);
        return bedList != null ?ResponseUtil.get(ResponseEnum.OK,pageInfo)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    @Transactional
    public ResponseUtil updateBed(Integer id) {
        Bed bed = bedMapper.selectByPrimaryKey(id);
        Integer wardId = bed.getWardId();
        int rows = bedMapper.updateBed(id);
        if (rows == 1){
            updateWardBedCount(wardId);
            WardWebSocket.sendToWard(wardId.toString(),
                    "{\"type\":\"bedDel\",\"bedId\":" + id + "}");
            return ResponseUtil.get(ResponseEnum.OK);
        }else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
//        return rows == 1
//                ? ResponseUtil.get(ResponseEnum.OK)
//                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    @Transactional
    public ResponseUtil insertSelective(Bed record) {
        int rows = bedMapper.insertSelective(record);
        if (rows == 1){
            updateWardBedCount(record.getWardId());
            WardWebSocket.sendToWard(record.getWardId().toString(),
                    "{\"type\":\"bedAdd\",\"bedNo\":\"" + record.getBedNo() + "\"}");
            return ResponseUtil.get(ResponseEnum.OK,record);
        }else {
            return ResponseUtil.get(ResponseEnum.FAIL);
        }
//        return rows == 1
//                ? ResponseUtil.get(ResponseEnum.OK, record)
//                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil updateByPrimaryKeySelective(Bed record) {
        int rows = bedMapper.updateByPrimaryKeySelective(record);
        WardWebSocket.sendToWard(record.getWardId().toString(),
                "{\"type\":\"bedStatus\",\"bedId\":" + record.getId() +
                        ",\"status\":" + record.getStatus() + "}");
        return rows == 1
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findByWardId(Integer wardId,Integer pageNum, Integer pageSize) {
//        List<Bed> bedList = bedMapper.findByWardId(wardId);
//        return bedList != null ? ResponseUtil.get(ResponseEnum.OK,bedList)
//                : ResponseUtil.get(ResponseEnum.FAIL);
        PageHelper.startPage(pageNum,pageSize);
        List<Bed> bedList = bedMapper.findByWardId(wardId);
        PageInfo pageInfo = new PageInfo(bedList);
        return bedList != null ?ResponseUtil.get(ResponseEnum.OK,pageInfo)
                : ResponseUtil.get(ResponseEnum.FAIL);

    }


    private void updateWardBedCount(Integer wardId){
        int bedCount = bedMapper.countBedWardId(wardId);
        Ward ward = new Ward();
        ward.setId(wardId);
        ward.setBedCount(bedCount);
        wardMapper.updateByPrimaryKeySelective(ward);

    }
}
